<template>
  <base-breadcrumb>
    <searchData
      :searchData="searchOptions"
      @search="handleSearch"
    />
    <!-- 表格 -->
    <a-card>
      <!-- <titleName title="双优化立项计划"></titleName> -->
      <div class="table-operator">
        <!-- <a-button type="primary" @click="addRouter">双优化立项情况</a-button> -->
      </div>
      <list-table
        ref="table"
        :request="loadData"
        :columns="columns"
        :exportConfig="{ use: true,select: '双优化立项计划',all: '双优化立项计划'}"
        @exportSelect="exportSelect"
        @exportAll="exportAll"
      >
<!--        <template-->
<!--          slot="prefix"-->
<!--          v-if="!isCopyInDesigns"-->
<!--        >-->
<!--          <a-button-->
<!--            type="primary"-->
<!--            icon="plus"-->
<!--            @click="handleAddPlan"-->
<!--            v-btnPermission="'tm_003_add_01'"-->
<!--          >新增上报</a-button>-->
<!--        </template>-->
        <template
          slot="description"
          slot-scope="text"
        >
          <ellipsis
            :length="8"
            tooltip
          >{{ text }}</ellipsis>
        </template>
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template
          slot="status"
          slot-scope="text, record"
        >
          <status-display :status-code="record.status" />
        </template>
          <template slot="currentVersion" slot-scope="text, record">
              <a @click="lookVersion(record)">V{{text}}</a>
          </template>
        <template
          slot="status"
          slot-scope="text, record"
        >
          <list-status
            :status="record.status"
            :isApproval="true"
            conversion="en"
            :text="text"
          ></list-status>
        </template>
        <!-- 操作 -->
        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a @click="addSee(record)">查看</a>
            <template v-if="!isCopyInDesigns">
              <a
                      @click="addEdit(record)"
                      v-btnPermission="'tm_003_cha_07'"
                      v-if="record.status == 'PAST'&&hideBuquBtn()"
              >变更</a>
              <a
                @click="addEdit(record)"
                v-btnPermission="'tm_003_edi_02'"
                v-if="hideBuquBtn() && (record.status == 'DRAFT' || record.status == 'REJECTED')"
              >编辑</a>
                <a
                  v-if="record.currentVersion>1&&(record.status == 'DRAFT' || record.status == 'REJECTED')&&hideBuquBtn()"
                  @click="discard(record)"
                  v-btnPermission="'tm_003_del_03'"

                >删除</a>
              </template>
            <a v-if="record.status && record.status !== 'DRAFT'"
                  @click="toggleApprovalDetails(record)"
            >审批信息</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
    <ApprovalDetails
      :visible="visible.approvalDetails"
      :businessId="childInitData.id"
      @cancel="visible.approvalDetails = false"
      @confirm="visible.approvalDetails = false"
    />
    <excel
      ref="exportSelect"
      file-name="双优化立项计划"
      is-click-generate="false"
    ><span slot="excelBtn"></span></excel>
    <excel
      ref="exportAll"
      file-name="双优化立项计划"
      is-click-generate="false"
    ><span slot="excelBtn"></span></excel>
      <version :visible="versionVisible" :versionId="versionId" @cancel="versionVisible=false"></version>
  </base-breadcrumb>
</template>

<script>
import ApprovalDetails from '@/components/approvalDetails'
import StatusDisplay from '@/components/StatusDisplay'
import { planList, eliminate } from '@/api/technical/optimizationPlan'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { typeListApproval } from '@/utils/util'
import version from "./version"
const searchOptions = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '名称',
  //   value: 'name',
  //   type: 'input'
  // },
  {
    name: '填报时间',
    value: 'dealDate',
    type: 'dateRange'
  },
  {
    name: '填报人',
    value: 'filledBy',
    type: 'input'
  },
  {
    name: '状态',
    value: 'status',
    type: 'select',
    data: [
      {
        value: '0',
        label: '待提交'
      },
      {
        value: '1',
        label: '审批中'
      },
      {
        value: '2',
        label: '审批通过'
      },
      {
        value: '3',
        label: '审批不通过'
      }
    ]
  }
]

const columns = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' }
  },
  // {
  //   title: '名称',
  //   dataIndex: 'name'
  // },
  {
    title: '所属分公司',
    dataIndex: 'branchOffice'
  },
  {
    title: '所属项目',
    dataIndex: 'project'
  },
  {
    title: '版本',
    dataIndex: 'currentVersion',
    scopedSlots: { customRender: 'currentVersion' }
  },
  {
    title: '总计划项数',
    dataIndex: 'planNum'
  },
  {
    title: '待立项数',
    dataIndex: 'approvalNum'
  },
  {
    title: '填报时间',
    dataIndex: 'submitDate'
  },
  {
    title: '填报人',
    dataIndex: 'filledBy'
  },
  {
    title: '状态',
    dataIndex: 'chiStatus',
    width: '150px'
  },
  {
    title: '操作',
    dataIndex: 'operation',
    width: '170px',
    scopedSlots: { customRender: 'operation' }
  }
]
const professionalTypeList = [
  {
    key: '建筑',
    value: 'CIVIL'
  },
  {
    key: '机电',
    value: 'ELECTRICAL'
  },
  {
    key: '结构',
    value: 'STRUCTURE'
  },
  {
    key: '市政景观',
    value: 'LANDSCAPE'
  }
]
export default {
  name: 'a' + Date.now(),
  components: {
    version,
    StatusDisplay,
    ApprovalDetails
  },
  data() {
    this.columns = columns
    return {
      versionVisible: false,
      isCopyInDesigns: location.href.includes('designs'), //是否是复制到设计中，如果是直接把操作按钮false掉
      childInitData: {},
      versionId: '',
      visible: {
        approvalDetails: false
      },
      loading: {
        add: false
      },
      searchOptions: searchOptions,
      reset: null,
      // 查询参数
      objFrom: {
        query: {}
      },
      dataSource: [], //数据列表
      queryParam: {},
      loadData: (parameter) => planList({ ...parameter, query: this.queryParam }),
      customColumns: {
        '分公司': 'branchOffice',
        '工程名称': 'project',
        '双优化分类': 'CcategoryName',
        '双优化名称': 'Cname',
        '内容概述': 'Ccontent',
        '设计阶段': 'CdesignStageName',
        '优化类别': 'CoptimizeStageName',
        '工程类别': 'CprojectType',
        '分部': 'Cpart',
        '创新': 'CisNew',
        '优化建议提出人': 'Cproposer',
        '拟实施人': 'Cimplement',
        '策划效益': 'Cbenefit',
        '专业类别': 'CprofessionalTypeName',
        '计划实施时间': 'CstartTime',
        '立项时间': 'CpropTime',
        '立项状态': 'CstatusName',
        '填报人（读取制表人数据）': 'filledBy',
        '填报时间': 'createTime'
      }
    }
  },
  mounted() {
    this.$refs.exportSelect.setExcelFields(this.customColumns)
    this.$refs.exportAll.setExcelFields(this.customColumns)
  },
  methods: {
    lookVersion(record) {
      this.versionId = record.id
      this.versionVisible = true
    },
    //处理列表数据成excel要的
    processList2Excel(list) {
      let newData = []
      list.map((v) => {
        if (v.doubleOptimizationTypeVO && v.doubleOptimizationTypeVO.length) {
          v.doubleOptimizationTypeVO.map((w) => {
            if(w.isNew == 1) {
              w.isNew = '是'
            } else {
              w.isNew = '否'
            }
           let o = professionalTypeList.find(el => el.value === w.professionalType)
            if(o) {
              w.professionalTypeName = o.key
            }
            if(w.status) {
              w.statusName = { PAST: '立项审批通过', REJECTED: '立项审批不通过', DRAFT: '立项待提交', SUBMITTED: '立项审核中' }[
                w.status
                ]
            }else {
              w.statusName = '待立项'
            }

            let temp = this.$clone(v)
            Object.keys(w).map((k) => {
              temp['C' + k] = w[k]
            })
            newData.push(temp)
          })
        } else {
          newData.push(this.$clone(v))
        }
      })
      return newData
    },
    exportSelect(e) {
      console.log(e,222)
      this.$refs.exportSelect.setDataAndGenerate(this.processList2Excel(e))
    },
    exportAll(e) {
      console.log(e,11)
      this.$refs.exportAll.setDataAndGenerate(this.processList2Excel(e))
    },
    toggleApprovalDetails(data) {
      this.childInitData = { ...data }
      this.visible.approvalDetails = true
    },
    async handleAddPlan() {
      this.$router.push({
        path: '/technical/addToReporting',
        query: { btn: true, btns: true }
      })
      // if (this.loading.add) return
      // this.loading.add = true
      // try {
      //   const params = {
      //     status: 'DRAFT',
      //   }
      //   const res = await addPlan(params)
      //   this.loading.add = false
      //   if (res.data.code === 200 || res.code === 200) {
      //     const { id } = res.data || {}
      //     this.$router.push({
      //       path: '/technical/addToReporting',
      //       query: { btn: true, id, btns: true },
      //     })
      //   }
      // } catch (e){
      //   console.log(e)
      //   this.loading.add = false
      // }
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val,
        createTimeBegin: (val.dealDate && val.dealDate.length && val.dealDate[0].format('YYYY-MM-DD')) || void 0,
        createTimeEnd: (val.dealDate && val.dealDate.length && val.dealDate[1].format('YYYY-MM-DD')) || void 0
      }
      this.$refs.table.refresh(true)
    },
    // 查看
    addSee(e) {
      this.$router.push({
        path: '/technical/planDetailed',
        query: { id: e.id }
      })
    },
    // 编辑
    addEdit(e) {
      this.$router.push({
        path: '/technical/addToReporting',
        query: { id: e.id, btns: true }
      })
    },
    addRouter() {
      this.$router.push('/technical/projectApproval')
    },
    discard(record) {
      let _this = this
      this.$confirm({
        title: '温馨提示',
        content: '请确认是否要删除当前版本的待提交数据，删除后将退回上一版本`？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          return eliminate({ id: record.id }).then((res) => {
            _this.$message.success('删除成功')
            _this.$refs.table.refresh() // 刷新
          })
        },
        onCancel() {}
      })
    }
    // // 删除
    // discard(e) {
    //     eliminate({
    //         id: e.id,
    //     }).then((res) => {
    //         if (res.data.code === 200 || res.code === 200) {
    //             this.$message.success('删除成功')
    //             this.$refs.table.refresh() // 刷新
    //         } else {
    //             this.$message.error('删除失败')
    //         }
    //     })
    // },
  }
}
</script>

<style></style>
